#include "stdafx.h"
#include <afxwin.h>
#include <afxinet.h>
#include <stdio.h>


static CInternetSession 	*pInetSession = NULL;
static CFtpConnection 		*pFtpConnection = NULL;

bool OpenConnection(CString server)
{
 if(server == "")
  return 0;
 
  pInetSession=new CInternetSession(AfxGetAppName(), 1, PRE_CONFIG_INTERNET_ACCESS);

  
 // put the server name in the CFtpGet class

 
 try {
  // try to connect to a ftp server
        pFtpConnection = pInetSession->GetFtpConnection(server);
      }
catch (CInternetException* pEx) 
     {
  // if failed, just show the error
  
  // Oops! We failed to connect!
  TCHAR szErr[1024];
  pEx->GetErrorMessage(szErr, 1024);
  TRACE(szErr);
  AfxMessageBox(szErr);
  pEx->Delete();
  return 0;// return 1 but previous error box have been showed
 }
 
 
 return 1;
}

////////////////////////////////////////////////////////////////////////////////////
bool CloseConnection()
{
 // close the connection to server, you can reconnect latter
 if(pFtpConnection == NULL)
  return 0;
 try{
  pFtpConnection->Close();
 }catch(...)
 {
  return 0;
 }
 if(pFtpConnection != NULL)
  delete pFtpConnection;
 
 if(pInetSession != NULL)
	delete pInetSession;
 
 return 1;
}

int PutFile(CString localfile, CString remotefile)
{
	return pFtpConnection->PutFile(localfile,remotefile);
}
